const app = getApp();
Page({

  data: {
    id: '1',
    ifenter: 0,
    department: '',
    isRegister: false,
    localmessage: [{
      username: 'xinmeitiweizhongxin',
      userpassword: 'qzxy123456',
      name: '新媒体微中心',
      picture: 'https://cloud-minapp-28782.cloud.ifanrusercontent.com/1htXzQFPt09iMdqd.jpg'
    }, {
      username: 'shijueshejibu',
      userpassword: 'qzxy123456',
      name: '视觉设计部',
      picture: 'https://cloud-minapp-28782.cloud.ifanrusercontent.com/1htXzQuex8gtlgJo.png'
    }, {
      username: 'gongguancehuabu',
      userpassword: 'qzxy123456',
      name: '公关策划部',
      picture: 'https://cloud-minapp-28782.cloud.ifanrusercontent.com/1htXzQjJXwjN4SSy.jpg'
    }, {
      username: 'qingzeweishi',
      userpassword: 'qzxy123456',
      name: '清泽微视',
      picture: 'https://cloud-minapp-28782.cloud.ifanrusercontent.com/1htXzQqzn1Y9EFuD.png'
    }, {
      username: 'xinwenzhongxin',
      userpassword: 'qzxy123456',
      name: '新闻中心',
      picture: 'https://cloud-minapp-28782.cloud.ifanrusercontent.com/1htXzQbHXYyCX8YZ.jpg'
    }, {
      username: 'zonghebangongshi',
      userpassword: 'qzxy123456',
      name: '综合办公室',
      picture: 'https://cloud-minapp-28782.cloud.ifanrusercontent.com/1htXzQLIXNc7GCGu.jpg'
    }, {
      username: 'jishukaifabu',
      userpassword: 'qzxy123456',
      name: '技术开发部',
      picture: 'https://cloud-minapp-28782.cloud.ifanrusercontent.com/1htXzQPN6hMymaKV.jpg'
    }, {
      username: 'wangluoanquanbu',
      userpassword: 'qzxy123456',
      name: '网络安全部',
      picture: 'https://cloud-minapp-28782.cloud.ifanrusercontent.com/1htXzQLy7vO7avZw.jpg'
    }],


  },
  change: function (e) {
    console.log(e.currentTarget.id)
    this.setData({
      id: e.currentTarget.id
    })

  },

  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: '首页'
    })
    var isRegister = wx.getStorageSync('isRegister')
    var department = wx.getStorageSync('department')
    var id = wx.getStorageSync('id')
    if (isRegister == true) {
      this.setData({
        department: department,
        isRegister: isRegister,
        ifenter: 1,
        userMessage: this.data.localmessage[id],
      })
    }
    this.setData({
      userMessage: this.data.localmessage[options.id],
      ifenter: options.ifenter
    })
    if (options.id == 0) {
      this.setData({
        department: '新媒体微中心'
      })
      wx.setStorageSync('department', this.data.department)
      wx.setStorageSync('id', options.id)
    }
    if (options.id == 1) {
      this.setData({
        department: '视觉设计部'
      })
      wx.setStorageSync('department', this.data.department)
      wx.setStorageSync('id', options.id)
    } if (options.id == 2) {
      this.setData({
        department: '公关策划部'
      })
      wx.setStorageSync('department', this.data.department)
      wx.setStorageSync('id', options.id)
    } if (options.id == 3) {
      this.setData({
        department: '清泽微视'
      })
      wx.setStorageSync('department', this.data.department)
      wx.setStorageSync('id', options.id)
    }
    if (options.id == 4) {
      this.setData({
        department: '新闻中心'
      })
      wx.setStorageSync('department', this.data.department)
      wx.setStorageSync('id', options.id)
    }
    if (options.id == 5) {
      this.setData({
        department: '综合办公室'
      })
      wx.setStorageSync('department', this.data.department)
      wx.setStorageSync('id', options.id)
    }
    if (options.id == 6) {
      this.setData({
        department: '技术开发部'
      })
      wx.setStorageSync('department', this.data.department)
      wx.setStorageSync('id', options.id)
    }
    if (options.id == 7) {
      this.setData({
        department: '网络安全部'
      })
      wx.setStorageSync('department', this.data.department)
      wx.setStorageSync('id', options.id)
    }
    app.globalData.department = this.data.department

    if (app.globalData.isRegister == true || isRegister == true) {
      let tableName = 'data'
      let query1 = new wx.BaaS.Query()
      let query2 = new wx.BaaS.Query()
      //是否属于第一志愿
      query1.compare('department1', '=', this.data.department)
      query1.compare('isPass1', '=', 1)

      //是否是第二志愿
      query2.compare('department2', '=', this.data.department)
      query2.compare('isPass1', '=', 0)
      query2.compare('isPass2', '=', 1)

      let orQuery = wx.BaaS.Query.or(query1, query2)

      let Product = new wx.BaaS.TableObject(tableName)
      Product.setQuery(orQuery).limit(1000).offset(0).find().then(res => {
        console.log(res)
        app.globalData.data = res.data.objects
        app.globalData.sum = res.data.objects.length
        console.log(app.globalData.data)
        //修改第一志愿被淘汰的信息
        var i;
        for (i = 0; i < app.globalData.sum; i++) {
          if (app.globalData.data[i].isPass1 == 0 && app.globalData.data[i].isPass2 == 1 && app.globalData.data[i].isPass == 0) {
            app.globalData.data[i].isUsed1 = 0;
            app.globalData.data[i].isUsed2 = 0;
            app.globalData.data[i].isUsed = 0;
            app.globalData.data[i].score = 0;
            app.globalData.data[i].tips = '';
            app.globalData.data[i].isPass == 1
            console.log(app.globalData.data[i])
            let tableName = 'data'
            let recordID = app.globalData.data[i].id // 数据行 id

            let Product = new wx.BaaS.TableObject(tableName)
            let product = Product.getWithoutData(recordID)
            product.set('isUsed2', parseFloat(0))
            product.set('isUsed', parseFloat(0))
            product.set('isUsed1', parseFloat(0))
            product.set('score', parseFloat(0))
            product.set('isPass', parseFloat(0))
            product.set('tips' , '')
            product.update().then(res => {
              console.log(res)
            }, err => {
              // err
            })
          }
        }
      }, err => {
        // err
      })

    }

  },
  charge: function (e) {
    if (this.data.userMessage != 0) {
      this.setData({ ifenter: 0 })
    }
  },
  // 跳转通过名单
  OnPassTap: function (event) {
    if (this.data.ifenter == 1 || this.data.isRegister == true) {
      wx.navigateTo({
        url: '/pages/pass_2/pass_2',
      })
    }
    else {
      wx.navigateTo({
        url: '/pages/register/register',
      })
    }
  },
  // 跳转淘汰名单
  OnOutTap: function (event) {
    if (this.data.ifenter == 1 || this.data.isRegister == true) {
      wx.navigateTo({
        url: '/pages/out_2/out_2',
      })
    }
    else {
      wx.navigateTo({
        url: '/pages/register/register',
      })
    }
  },
  // 跳转面试名单
  OnInterviewTap: function (event) {
    if (this.data.ifenter == 1 || this.data.isRegister == true) {
      wx.navigateTo({
        url: '/pages/interview/interview',
      })
    }
    else {
      wx.navigateTo({
        url: '/pages/register/register',
      })
    }
  },
  // 跳转报名名单
  OnEnrollTap: function (event) {
    if (this.data.ifenter == 1 || this.data.isRegister == true) {
      wx.navigateTo({
        url: '/pages/enroll/enroll',
      })
    }
    else {
      wx.navigateTo({
        url: '/pages/register/register',
      })
    }
  },
  enter: function () {
    wx.navigateTo({
      url: '../register/register',
    })


  },
})